{*****************************************************************************}
{* A simple bubble sort program.  Reads integers, one per line, and prints   *}
{* them out in sorted order.  Blows up if there are more than 49.            *}
{*****************************************************************************}
PROGRAM Sort(input, output);
	CONST
	{* Max array size. *}
	MaxElts = 50;
    TYPE 
	{* Type of the element array. *}
	IntArrType = ARRAY [1..MaxElts] OF Integer;

    VAR
	{* Indexes, exchange temp, array size. *}
	i, j, tmp, size: Integer;

	{* Array of ints *}
	arr: IntArrType;

    {* Read in the integers. *}
    PROCEDURE ReadArr(VAR size: Integer; VAR a: IntArrType);
	VAR
	total: Integer;
	BEGIN
		write('Enter length:');
		read(total);
		IF total <= MaxElts THEN BEGIN
			size := 1;
			write('Enter elements:');
			WHILE size <= total DO BEGIN
				read(a[size]);
				size := size + 1;
			END;
		END
		ELSE BEGIN
			write('Out of bound.');
		END;
	END;

    BEGIN
	{* Read *}
	ReadArr(size, arr);

	{* Sort using bubble sort. *}
	FOR i := size - 1 DOWNTO 1 DO
	    FOR j := 1 TO i DO 
		IF arr[j] > arr[j + 1] THEN BEGIN
		    tmp := arr[j];
		    arr[j] := arr[j + 1];
		    arr[j + 1] := tmp;
		END;

	{* Print. *}
	writeln('Sorted result is:');
	FOR i := 1 TO size DO
	    writeln(arr[i]);
    END.
